//
//  UserInfoViewController.m
//  TLBS-Project
//
//  Created by 123 on 13-12-20.
//  Copyright (c) 2013年 Wei WenRu. All rights reserved.
//
#import "TRChatViewController.h"
#import "UserInfoViewController.h"
#import "UserInfo.h"
#import "WeiboPaser.h"
#import "TRWeiboListViewController.h"

@interface UserInfoViewController ()
@property (nonatomic, strong)UserInfo *userInfo;
@end

@implementation UserInfoViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.app = [UIApplication sharedApplication].delegate;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    
    self.headBg.layer.cornerRadius = 45;  //圆弧半径
    self.headBg.layer.borderWidth = .5;
    self.headBg.layer.borderColor = [UIColor grayColor].CGColor;
    self.headBg.layer.masksToBounds = YES;
    
    //从网络获取用户信息
    if (self.userName) {
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:@"json" forKey:@"format"];
        [params setObject:self.userName forKey:@"name"];
        [self.app.weiboApi requestWithParams:params  apiName:@"user/other_info" httpMethod:@"GET" delegate:self];
    }else{
        //从网络获取用户信息
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:@"json" forKey:@"format"];
        [self.app.weiboApi requestWithParams:params  apiName:@"user/info" httpMethod:@"GET" delegate:self];
    }
}

-(void)viewDidAppear:(BOOL)animated{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.app.weiboApi loginWithDelegate:self andRootController:self];
    });
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)jumpTo:(UIButton *)sender {
    switch (sender.tag) {
        case 0: //herTopic
        {
            TRWeiboListViewController *weibosVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WeibosVc"];
            weibosVC.userInfo = self.userInfo;
            [self.navigationController pushViewController:weibosVC animated:YES];
        }
            break;
            
        case 1: //sendMessage
        {
            UINavigationController *na = [self.storyboard instantiateViewControllerWithIdentifier:@"chatvc"];
            TRChatViewController *cvc = na.viewControllers[0];
            cvc.sendToUserInfo = self.userInfo;
            [self presentViewController:na animated:YES completion:Nil];
            
        }
            break;

    }
}

#pragma mark weiboDelegate
- (void)didReceiveRawData:(NSData *)data reqNo:(int)reqno{
    
       NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
    self.userInfo = [WeiboPaser paseUserInfoByJsonString:strResult];
    
    [self updateUI];
}
-(void)updateUI{
 
  //设置头像
    UIImageView* userImageView = [[UIImageView alloc]initWithFrame:CGRectMake(120, -40, 80, 80)];
    
    userImageView.backgroundColor = [UIColor clearColor];
    userImageView.layer.cornerRadius = 40;  //圆弧半径
    userImageView.layer.borderWidth = .5;
    userImageView.layer.borderColor = [UIColor grayColor].CGColor;
    userImageView.layer.masksToBounds = YES;
    [self.backView addSubview:userImageView];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.userInfo.head]];
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            userImageView.image = image;
        });
    });

    
    self.name.text = self.userInfo.name;
    self.nickName.text = self.userInfo.nick;
    self.sex.text = [NSString stringWithFormat:@"%@", self.userInfo.sex?@"男":@"女"];
    self.age.text = self.userInfo.age;
    self.birthday.text = self.userInfo.birthday;
    self.company.text = self.userInfo.company;
    self.introductionTV.text = self.userInfo.introduction;
}
@end
